﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class WebUserControl_WebUserControl : System.Web.UI.UserControl
{
    static string sitecode = "";
    static string username = "";
    BCDataCenter.DataAccessLayer.DAL dt;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string MMyyyy="012010";
        username = Session["Username"].ToString();
        sitecode = Session["Sitecode"].ToString();
        dt = new BCDataCenter.DataAccessLayer.DAL();
        //try
        //{
        //    dt.MyExecuteNonQuery("P_CreateMonthlyAwards", CommandType.StoredProcedure, ref error, new SqlParameter("@MMyyyy", MMyyyy));
        //    dt.MyExecuteNonQuery("P_InsertHHtructiep", CommandType.StoredProcedure, ref error, new SqlParameter("@sitecode", sitecode), new SqlParameter("@MMyyyy", MMyyyy));
        //    dt.MyExecuteNonQuery("P_CreateTMPHH", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
        //    dt.MyExecuteNonQuery("P_InsertTMPHH", CommandType.StoredProcedure, ref error, new SqlParameter("@sitecode", sitecode), new SqlParameter("@MMyyyy", MMyyyy), new SqlParameter("@username", username));
        //    dt.MyExecuteNonQuery("P_InsertHHgiantiep", CommandType.StoredProcedure, ref error, new SqlParameter("@sitecode", sitecode), new SqlParameter("@MMyyyy", MMyyyy), new SqlParameter("@username", username));
        dt.MyExecuteNonQuery("P_createHHQLTMP1", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
        //    dt.MyExecuteNonQuery("P_insertBUM", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username), new SqlParameter("@sitecode", sitecode));
        //}
        //catch (Exception ex)
        //{
        //    uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
        //}
        DataSet ds = new DataSet();
        //ds = dt.MyExecuteQuery(ref error, "P_selectBUM_TMP1", CommandType.StoredProcedure, new SqlParameter("@username", username));
        //int rownum = ds.Tables[0].Rows.Count;
         try
         {
            //    for (int i = 0; i < rownum; i++)
            //    {           
            //        string SBUM = dt.MyExecuteQuerys(ref error, "GetSBUM", CommandType.StoredProcedure, new SqlParameter("@SalespKey", ds.Tables[0].Rows[i][0].ToString()));
            //        dt.MyExecuteNonQuery("P_updateSBUMtoTMP1", CommandType.StoredProcedure, ref error, new SqlParameter("@SBUM", SBUM), new SqlParameter("@BUM", ds.Tables[0].Rows[i][0].ToString()), new SqlParameter("@username", username));
            //        string doanhsoNCD = dt.MyExecuteQuerys(ref error, "P_GetDSNhomCN", CommandType.StoredProcedure, new SqlParameter("@BUM", ds.Tables[0].Rows[i][0].ToString()), new SqlParameter("@MMyyyy", MMyyyy));
            //        if (doanhsoNCD == "")
            //            doanhsoNCD = "0";
            //        dt.MyExecuteNonQuery("P_updateDSNhomCN", CommandType.StoredProcedure, ref error, new SqlParameter("@DSNhomCN", float.Parse(doanhsoNCD)), new SqlParameter("@BUM", ds.Tables[0].Rows[i][0].ToString()), new SqlParameter("@username", username));
            //        dt.MyExecuteNonQuery("P_CreateTMP4", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
            //        dt.MyExecuteNonQuery("P_insertNhanhchuan", CommandType.StoredProcedure, ref error, new SqlParameter("@BUM", ds.Tables[0].Rows[i][0].ToString()), new SqlParameter("@username", username));
            //        string Doanhsotrunggian =dt.MyExecuteQuerys(ref error,"P_GetDSTG",CommandType.StoredProcedure,new SqlParameter("@MMyyyy",MMyyyy),new SqlParameter("@username",username));
            //        if (Doanhsotrunggian == "")
            //            Doanhsotrunggian = "0";
            //        dt.MyExecuteNonQuery("P_updateDSTG", CommandType.StoredProcedure, ref error, new SqlParameter("@DSTG", float.Parse(Doanhsotrunggian)), new SqlParameter("@BUM", ds.Tables[0].Rows[i][0].ToString()), new SqlParameter("@username", username));
                
            //    }
            //ds.Dispose();
            //dt.MyExecuteNonQuery("P_updateSNC", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
             //ClassForPromote CPr = new ClassForPromote();
             //CPr.ForPromote(username, MMyyyy);
             dt.MyExecuteNonQuery("ls_PrepareDataForPromote", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username), new SqlParameter("@MmYyyy", MMyyyy));
             dt.MyExecuteNonQuery("ls_PrepareDataForPromote1", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username), new SqlParameter("@MmYyyy", MMyyyy));
             dt.MyExecuteNonQuery("ls_PrepareDataForPromote2", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username), new SqlParameter("@MmYyyy", MMyyyy));
             dt.MyExecuteNonQuery("ls_PrepareDataForPromote3", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username), new SqlParameter("@MmYyyy", MMyyyy));
             dt.MyExecuteNonQuery("ExecStoreReport", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username));
             dt.MyExecuteNonQuery("ls_GenerateExtraSalesIndex", CommandType.StoredProcedure, ref error, new SqlParameter("@UserName", username));
             dt.MyExecuteNonQuery("P_insert_ThuongQuanLy_Temp1", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
            dt.MyExecuteNonQuery("P_PassOver", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
            dt.MyExecuteNonQuery("P_UpdateLoaiNhan", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
            ds=dt.MyExecuteQuery(ref error,"P_getTMP1",CommandType.StoredProcedure,new SqlParameter("@username",username));
            int  rownum = ds.Tables[0].Rows.Count;
             while(rownum>=1)
             {
                 try
                 {
                     dt.MyExecuteNonQuery("P_GenerateThuongQuanLy", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
                     ds = dt.MyExecuteQuery(ref error, "P_getTMP1", CommandType.StoredProcedure, new SqlParameter("@username", username));
                     rownum = ds.Tables[0].Rows.Count;
                 }
                 catch
                 {
                     return;
                 }
             }
             dt.MyExecuteNonQuery("P_GenerateLuongTheoDoanhSo", CommandType.StoredProcedure, ref error, new SqlParameter("@username", username));
             dt.MyExecuteNonQuery("P_InsertHHQL", CommandType.StoredProcedure, ref error, new SqlParameter("@MMyyyy", MMyyyy));
             dt.MyExecuteNonQuery("P_InsertHHDS", CommandType.StoredProcedure, ref error, new SqlParameter("@MMyyyy", MMyyyy));
             dt.MyExecuteNonQuery("P_InsertHHDS1", CommandType.StoredProcedure, ref error, new SqlParameter("@MMyyyy", MMyyyy));
             dt.MyExecuteNonQuery("P_InsertHHDS2", CommandType.StoredProcedure, ref error, new SqlParameter("@MMyyyy", MMyyyy));
            }                    
         catch (Exception ex)
         {
             uscMsgBox1.AddMessage(ex.Message, uscMsgBox.enmMessageType.Error);
         }
    }
}
